﻿#pragma warning disable CS8602 // Dereference of a possibly null reference.

namespace Common.Extensions
{
    public static class ArrayExtension
    { /// <summary>
      /// 数组切片 </summary> <typeparam name="T"> </typeparam> <param name="source"> </param> <param
      /// name="start"> </param> <param name="end"> </param> <returns> </returns>
        public static T[] Slice<T>(this T[] source, int start, int end)
        {
            if (end < 0)
            {
                end = source.Length + end;
            }
            int len = end - start;
            T[] ret = new T[len];
            for (int i = 0; i < len; i++)
            {
                ret[i] = source[start + i];
            }
            return ret;
        }

        /// <summary>
        /// 数组比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <returns></returns>
        public static bool ValueEqual<T>(this T[] source, T[] dest)
        {
            if (dest.Length != source.Length)
            {
                return false;
            }
            for (int i = 0; i < source.Length; i++)
            {
                if (!dest[i].Equals(source[i]))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// byte 索引
        /// </summary>
        /// <param name="source"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static bool Index(this byte[] source, int i)
        {
            return (source[i / 8] & (1 << (i % 8))) > 0;
        }
    }
}